#include <iostream>
#include <pthread.h>
#include <cerrno>
#include <string.h>
#include <unistd.h>
#include <string>
#include <thread>
#include "Thread.hpp"
#include "lockGuard.hpp"
using namespace std;

// void threadRun(void* args)
// {
//     string message = static_cast<const char*> (args);
//     int cnt = 10;
//     while(cnt)
//     {
//         cout<<"我是一个线程："<<message<<",cnt："<<cnt<<endl;
//         cnt--;
//         sleep(1);
//     }
// }

// int main()
// {
//     Thread t1(1,threadRun,(void*)"hellobit1");
//     Thread t2(2,threadRun,(void*)"hellobit2");
//     cout<<"threadname:"<<t1.threadname()<<" threadid:"<<t1.threadid()<<" threadstatus:"<<t1.threadstatus()<<endl;
//     cout<<"threadname:"<<t2.threadname()<<" threadid:"<<t2.threadid()<<" threadstatus:"<<t2.threadstatus()<<endl;
//     cout<<"t1 and t2 begin runing"<<endl;
//     t1.run();
//     t2.run();
//     cout<<"threadname:"<<t1.threadname()<<" threadid:"<<t1.threadid()<<" threadstatus:"<<t1.threadstatus()<<endl;
//     cout<<"threadname:"<<t2.threadname()<<" threadid:"<<t2.threadid()<<" threadstatus:"<<t2.threadstatus()<<endl;
//     t1.join();
//     t2.join();
//     cout<<"threadname:"<<t1.threadname()<<" threadid:"<<t1.threadid()<<" threadstatus:"<<t1.threadstatus()<<endl;
//     cout<<"threadname:"<<t2.threadname()<<" threadid:"<<t2.threadid()<<" threadstatus:"<<t2.threadstatus()<<endl;
//     return 0;
// }

// class ThreadData
// {
// public:
//       //构造
//       ThreadData(string name,pthread_mutex_t* mutex):_name(name),_mutex(mutex)
//       {}
//       //析构
//       ~ThreadData()
//       {}
// public:
//   string _name; //线程名
//   pthread_mutex_t* _mutex; //这个锁的地址
// };

int tickets = 1000;
//pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  //定义全局锁
pthread_mutex_t mutex;
void *RouTine(void *args)
{
    string name = static_cast<const char *>(args);
    // ThreadData* td = static_cast<ThreadData*> (args);
    // 线程进行抢票
    while (true)
    {
        // 加锁
        // pthread_mutex_lock(&mutex);
        {
            LockGuard lockguard(&mutex);  //更加优雅的加锁与解锁 -->RAII 风格的锁
            if (tickets > 0) // 有票继续抢
            {
                usleep(2000); // 模拟线程抢票的等待时间
                cout << name << "get a ticket：" << tickets-- << endl;
                // 解锁
                // pthread_mutex_unlock(&mutex);
            }
            else
            { // 没有票退出
              // 解锁
                // pthread_mutex_unlock(&mutex);  //先解锁，再break退出
                break;
            }
        }
        usleep(100); // 模拟抢完一张票之后执行后续操作
    }
    return nullptr;
}

int main()
{
    // pthread_mutex_t mutex;  //局部锁
    // pthread_mutex_init(&mutex,nullptr);  //对锁进行初始化
    pthread_t t[4]; // 定义线程标识符
    for (int i = 0; i < 4; i++)
    {
        char *name = new char[64];
        // char name[64];
        snprintf(name, 64, "thread-%d", (i + 1));
        // ThreadData* td = new ThreadData(name,&mutex);
        pthread_create(t + i, nullptr, RouTine, name); // 创建线程
    }

    // 等待线程
    for (int i = 0; i < 4; i++)
    {
        int n = pthread_join(t[i], nullptr); // 无需返回值，传nullptr
        if (n != 0)
        {
            cerr << "error:" << n << strerror(n) << endl;
        }
    }
    // 使用完对锁进行销毁
    // pthread_mutex_destroy(&mutex);
    return 0;
}